import copy
import re

from tool_class.page_handle_base import PageHandleBase


class PageHandle(PageHandleBase):
    def __init__(self, page):
        super().__init__(page)

    def query_handle(self,data):
        """
        goto_local
        pull_checkbox
        pull_checked
        pull_cascade
        input
        """
        result = {}
        for key in data :
            method,name = key.split(".")
            method_to_call = getattr(self, method)
            result[name] = method_to_call(**data[key])
        print(result)
        return result

    def edit_handle(self,data):

        return self.query_handle(data)

    def add_handle(self,data):

        return self.query_handle(data)

    def sort_handle(self,sort_type,data_element):
        sort_obj,sort_el = data_element
        raw_data = self.get_list_data(sort_el)
        if sort_type == '升序':
            self.page.locator(f'th span span.arco-table-th-title:has-text("{sort_obj}")').nth(1).click()
            sort_data = self.get_list_data(sort_el)
            return sort_data, raw_data
        elif sort_type == '降序':
            element = self.page.locator(f'th span span.arco-table-th-title:has-text("{sort_obj}")').nth(-1)
            element.click()
            element.click()
            sort_data = self.get_list_data(sort_el)
            return sort_data, raw_data


class DataTreatingPageHandle(PageHandle):

    def __init__(self, page):
        super().__init__(page)

    def multiple_pull(self,condition):
        result = {}
        for key in condition:
            if key == '条件详情':
            #条件详情选择时，如果条件类型不是资源组，则value为输入框
                if  condition['条件类型'][1][-2:] != '-1' or condition['变量值'][1][-2:] == '-1':
                    value = self.input(condition[key][0],condition[key][1])
                else:
                    value = self.pull_checked(condition[key][0], condition[key][1])
            elif key == '动作_条件详情':
                if  condition['动作_条件类型'][1][-2:] != '-1':
                    value = self.input(condition[key][0],condition[key][1])
                else:
                    value = self.pull_checked(condition[key][0], condition[key][1])
            else:
                value = self.pull_checked(condition[key][0], condition[key][1])
            result[key] = value
        return result

    def multiple_field_edit(self,field_value):
        result = {}
        for key in field_value:
            if key == "规则名称" or key == "规则描述":
                value = self.input(field_value[key][0], field_value[key][1])
            elif key == "处理阶段" or key == "规则类型" or key == "条件限制":
                value = self.radio_select(field_value[key][0], field_value[key][1])
            elif key == "数据渠道":
                value = self.pull_cascade(field_value[key][0], field_value[key][1])
            elif key == "新增条件" or key == "新增动作" \
                    or key == "刪除动作" or key == "刪除条件":
                print(key,'执行',key)
                value = self.button_click(testId = field_value[key])
            result[key] = value
        return result

    def edit_handle(self,data):
        copied_dict = copy.deepcopy(data)
        copied_dict['multiple_field_edit.其余字段']['field_value'].pop('数据渠道')
        return super().edit_handle(copied_dict)

"""Field processing"""
class LabelManagePageHandle(PageHandle):


    def field_processing(self,field_obj):
        result = {}
        for key in field_obj:
            if key == '标签名称' or key == '英文名称' or key == '标签定义' or key == '分类名称':
                value = self.input(field_obj[key][0],field_obj[key][1])
            elif key == '所属分类':
                value = self.pull_cascade(field_obj[key][0],field_obj[key][1])
            elif key == '标签类型':
                value = self.pull_checked(field_obj[key][0],field_obj[key][1])
            elif key == '标签属性' or key == '是否启用':
                value = self.radio_select(field_obj[key][0], field_obj[key][1])
            else:
                value = self.checkbox(*field_obj[key])
            result[key] = value
        return result

    def sort_handle(self,sort_type,data_element):
        sort_obj,sort_el = data_element
        raw_data = self.get_list_data(sort_el)
        if sort_type == '升序':
            self.page.locator(f'th span span.arco-table-th-title:has-text("{sort_obj}")').click()
            self.page.wait_for_selector(f'[data-testid*="{sort_el}"]',timeout=30000)
            sort_data = self.get_list_data(sort_el)
            return sort_data, raw_data
        elif sort_type == '降序':
            element = self.page.locator(f'th span span.arco-table-th-title:has-text("{sort_obj}")').nth(-1)
            element.click()
            element.click()
            self.page.wait_for_selector(f'[data-testid*="{sort_el}"]',timeout=30000)
            sort_data = self.get_list_data(sort_el)
            return sort_data, raw_data

